/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package trace;

/**
 *
 * @author Julien Cassis
 */
public class Branch {
    private long address;
    private int MAX_BUFFER;
    private int predictionBuffer;
    private int misPrediction;
    private int count;
    private int bits;

    public Branch(int bits) {
        this.bits = bits;
        this.MAX_BUFFER = (1<<bits) - 1;
    }

    public long getAddress(){
        return this.address;
    }

    public int getCounter(){
        return this.predictionBuffer;
    }

    public int getMisPrediction(){
        return this.misPrediction;
    }

    public int getCount(){
        return this.count;
    }

    public int getPrediction(){
        if(this.predictionBuffer < (Math.pow(2,this.bits)/2)) {
            return 0;
        }else{
            return 1;
        }
    }

    public void incrementBuffer() {
        if (predictionBuffer < MAX_BUFFER) {
            predictionBuffer++;
        }
    }

    public void decrementBuffer() {
        if (predictionBuffer > 0) {
            predictionBuffer--;
        }
    }

    public int predictionBufferUpdate(int outcome){
        int prediction = this.getPrediction();
        if (prediction == outcome) {
            // Your prediction was right, celebrate
        } else {
            // Your prediction was wrong, count it
            misPrediction++;
        }
        if (outcome == 1) {
            incrementBuffer();
        } else {
            decrementBuffer();
        }
        this.count++;
        return prediction;
    }

}
